#include <iostream>
#include <string>
#include "Protocol.hpp"
#include "NetCal.hpp"
#include "Reactor.hpp"
#include "Listener.hpp"
#include "Log.hpp"
#include "Command.hpp"

static void Usage()
{
    std::cout << "输入错误，请输入./ip port" << std::endl;
}
int main(int argc, char *argv[])
{
    if (argc != 2)
    {

        Usage();
        exit(USAGE_ERR);
    }
    uint16_t port = std::stoi(argv[1]);
    LogModule::ConsolelogStrategy();
    // 构建业务模块
    std::shared_ptr<Cal> cal = std::make_shared<Cal>();
    // 构建协议模块
    std::shared_ptr<Protocol> pro = std::make_shared<Protocol>([&cal](Request &req)
                                                               { return cal->Execute(req); });
    // 构建监听模块
    std::shared_ptr<Connection> conn = std::make_shared<Listener>(port);

    conn->RegistHandler([&pro](std::string inbuffer) -> std::string
                        {
        LOG(LogLevel::DEBUG)<<"进入到匿名函数模块。。。。。";
        std::string send_str;
        while(true)
        {
            std::string package;
        if(!pro->Decode(inbuffer,&package))
        break;
        send_str+=pro->excute(package);
        }
         LOG(LogLevel::DEBUG)<<"结束匿名函数模块。。。。。";
        return send_str; });
    // 构建Reactor模块，做事件派发
    std::unique_ptr<Reactor> R = std::make_unique<Reactor>();
    R->AddConnection(conn);
    R->Loop();
}

